﻿using System;
using System.Collections;
using System.Windows;
using TransControlSystem.Model;
using TransControlSystem.View.Common;

namespace TransControlSystem.ViewModel.Common
{
    /// <summary>
    /// 窗口管理器
    /// </summary>
    public static class WindowManager
    {
        private static Hashtable _RegisterWindow = new Hashtable();
        public static void Register<T>(string key)
        {
            if (!_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, typeof(T));
            }
        }

        public static void Register(string key, Type t)
        {
            if (!_RegisterWindow.Contains(key))
            {
                _RegisterWindow.Add(key, t);
            }
        }

        public static void Remove(string key)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                _RegisterWindow.Remove(key);
            }
        }

        /// <summary>
        /// 普通打开
        /// </summary>
        /// <param name="key"></param>
        /// <param name="VM"></param>
        public static void Show(string key, object VM)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                win.Show();
            }
        }

        /// <summary>
        /// 模态窗口打开
        /// </summary>
        /// <param name="key"></param>
        /// <param name="VM"></param>
        public static void ShowDialog(string key, object VM)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                win.ShowDialog();
            }
        }

        /// <summary>
        /// 模态窗口打开确认框
        /// </summary>
        /// <param name="key">要打开的窗体</param>
        /// <param name="title">提示标题</param>
        /// <param name="content">提示内容</param>
        /// <param name="CallbackMessage">回调信息</param>
        public static void ConfirmShow(string title, string content, string CallbackMessage)
        {
            string key = "ConfirmWindow";
            if (_RegisterWindow.ContainsKey(key))
            {
                ConfirmWindow win = (ConfirmWindow)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                ConfirmViewModel confirmWindowModelView = (ConfirmViewModel)win.DataContext;
                confirmWindowModelView.Title = title;
                confirmWindowModelView.Content = content;
                confirmWindowModelView.CallbackMessage = CallbackMessage;
                win.ShowDialog();
            }
        }

        /// <summary>
        /// 模态窗口打开确认框
        /// </summary>
        /// <param name="key">要打开的窗体</param>
        /// <param name="title">提示标题</param>
        /// <param name="content">提示内容</param>
        /// <param name="CallbackMessage">回调信息</param>
        public static void ShowWarning(string title, string content)
        {
            string key = "WarningWindow";
            if (_RegisterWindow.ContainsKey(key))
            {
                WarningWindow win = (WarningWindow)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                WarningViewModel warningWindowViewModel = (WarningViewModel)win.DataContext;
                warningWindowViewModel.Title = title;
                warningWindowViewModel.Content = content;
                win.ShowDialog();
            }
        }

        /// <summary>
        /// 模态窗口打开编辑用户窗体
        /// </summary>
        /// <param name="key"></param>
        /// <param name="VM"></param>
        public static void ShowEdit(string key, object param)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                EditUserViewModel editUser = (EditUserViewModel)win.DataContext;
                editUser.LoadUserInfo((User)param);
                win.ShowDialog();
            }
        }
        /// <summary>
        /// 模态窗口打开编辑程序块值的窗体
        /// </summary>
        /// <param name="key"></param>
        /// <param name="VM"></param>
        public static void ShowEditValue(string key, TagInfo param)
        {
            if (_RegisterWindow.ContainsKey(key))
            {
                var win = (Window)Activator.CreateInstance((Type)_RegisterWindow[key]);
                win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                EditValueWindowViewModel tagInfoEdit = (EditValueWindowViewModel)win.DataContext;
                tagInfoEdit.LoadTagInfo(param);
                win.ShowDialog();
            }
        }


    }
}
